package com.googlecode.weathergram;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.googlecode.weathergram.WeatherService.WeatherBinder;
import com.googlecode.weathergram.WeatherService.WeatherData;

public class ShareActivity extends Activity {

	private WeatherBinder weatherBinder;
	private ServiceConnection weatherConn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			weatherBinder = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			weatherBinder = (WeatherBinder) service;
			final Timer t = new Timer();

			t.scheduleAtFixedRate(new TimerTask() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (weatherBinder.isFullyLoaded()) {
								renderImage();
								t.cancel();
							}
						}
					});
				}
			}, 1000, 1000);
		}
	};

	private ImageView imageView;
	private Button shareButton;
	private Uri finalImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitNetwork().build();
		StrictMode.setThreadPolicy(policy);

		Intent weather = new Intent(this, WeatherService.class);
		bindService(weather, weatherConn, BIND_AUTO_CREATE);

		imageView = (ImageView) findViewById(R.id.imageView);
		shareButton = (Button) findViewById(R.id.shareButton);
		shareButton.setEnabled(false);
		shareButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (finalImage != null) {
					Intent share = new Intent(Intent.ACTION_SEND);

					share.setType("image/*");
					share.putExtra(Intent.EXTRA_STREAM, finalImage);

					startActivity(Intent.createChooser(share,
							getString(R.string.share_shareChooser)));
				}
			}
		});
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (weatherBinder != null)
			unbindService(weatherConn);
	}

	public void renderImage() {
		WeatherData data = weatherBinder.getData();

		Uri origImage = Uri.parse((String) getIntent().getExtras().get("mediaUri"));
		finalImage = applyDataToImage(data, origImage);

		imageView.setImageURI(finalImage);
		shareButton.setEnabled(true);
	}

	private Uri applyDataToImage(WeatherData data, Uri imageUri) {

		try {
			Bitmap src = MediaStore.Images.Media.getBitmap(this.getContentResolver(),
					imageUri);
			Bitmap working = Bitmap.createBitmap(src);
			Bitmap image = working.copy(Bitmap.Config.ARGB_8888, true);

			int height = image.getHeight();
			int width = image.getWidth();

			Canvas canvas = new Canvas(image);

			Paint paint = new Paint();
			Typeface typeface = Typeface.createFromAsset(getAssets(), "Aller_Bd.ttf");
			paint.setTypeface(typeface);
			paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

			canvas.drawBitmap(image, 0, 0, paint);

			paint.setColor(Color.BLACK);
			paint.setAlpha(75);
			canvas.drawRect(new Rect(0, height / 2, width, height), paint);

			canvas.drawRect(new Rect(0, height / 2, 135, (height / 2) + 60), paint);

			paint.setColor(Color.WHITE);
			paint.setAlpha(255);
			paint.setTextSize(25);
			canvas.drawText("I'M HERE", 15, (height / 2) + 40, paint); // TODO
																																	// localize
			paint.setTextSize(20);
			String timeStamp = new SimpleDateFormat("HH:mm - dd. MMMM yyyy",
					Locale.getDefault()).format(new Date());
			canvas.drawText(timeStamp, 155, (height / 2) + 40, paint);

			paint.setTextSize(50);
			canvas.drawText(data.getCity(), 15, (height / 2) + 125, paint);

			paint.setTextSize(40);
			canvas.drawText(data.getTemp(), 15, (height / 2) + 200, paint);

			return ImageHelper.saveImage(image, "final");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
